'
该节点用于时时保存游戏内数据
'

extends Node

const SAVE_FILE = "user://Save/"#固定保存总文件夹
const PATH =  SAVE_FILE + "gamesave.json"#游戏内容文件路径

#游戏内容数据
var save_data: Dictionary = {
	"experience_number": 0,
	"external_upgrade": {}
}

#加载游戏内数据文件：数据字典
func load_save() -> void:
	if !FileAccess.file_exists(PATH):
		return
	var file = FileAccess.open(PATH, FileAccess.READ)
	save_data = file.get_var()
	pass

#保存数据
func save() -> void:
	if !DirAccess.dir_exists_absolute(SAVE_FILE):
		DirAccess.make_dir_absolute(SAVE_FILE)
	var file = FileAccess.open(PATH, FileAccess.WRITE)
	file.store_var(save_data)
	pass

#增加一种局外升级
func add_external_upgrade(upgrade: ExternalUpgrade) -> void:
	if !save_data.external_upgrade.has(upgrade.id):
		save_data.external_upgrade[upgrade.id] = {
			"quantity": 0
		}
	save_data.external_upgrade[upgrade.id].quantity += 1
	self.save()
	pass

#返回传入升级ID的等级
func get_upgrade_quantity(upgrade_id:String) -> int:
	if save_data.external_upgrade.has(upgrade_id):
		return save_data.external_upgrade[upgrade_id].quantity
	return 0

#返回查询物品数量
func get_quantity(id:String) -> int:
	if save_data.has(id):
		return save_data[id]
	return 0

#增加经验数量
func experience_collected(number:int) -> void:
	save_data.experience_number += number
	save()
	pass
